package com.zhl.emp_manage.controller;

import com.zhl.emp_manage.entity.Dept;
import com.zhl.emp_manage.service.DeptService;
import com.zhl.emp_manage.utils.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "部门信息管理")
@RestController
@RequestMapping("/emp_manage/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门
     *
     * @return
     */
    @ApiOperation("查询所有部门")
    @GetMapping("/depts")
    public Results getAllDept() {
        List<Dept> allDept = deptService.getAllDept();
        return Results.ok().data("list", allDept);
    }

    /**
     * 通过id编号查询
     *
     * @param id
     * @return
     */
    @ApiOperation("通过id查询")
    @GetMapping("/depts/{id}")
    public Results getByDeptNo(@PathVariable Integer id) {
        Dept byDeptNo = deptService.getByDeptNo(id);
        return Results.ok().data("dept", byDeptNo);
    }

    /**
     * 新增部门
     *
     * @param dept
     * @return
     */
    @ApiOperation("新增部门")
    @PostMapping("/depts")
    public Results addDept(@RequestBody Dept dept) {
        boolean b = deptService.addDept(dept);
        return Results.ok().message("添加成功");
    }

    /**
     * 修改部门信息
     *
     * @param dept
     * @return
     */
    @ApiOperation("根据id修改部门信息")
    @PostMapping("/depts/{id}")
    public Results updateDept(@RequestBody Dept dept, @PathVariable Integer id) {
        boolean b = deptService.updateDept(dept);
        return Results.ok().message("修改成功");
    }

    /**
     * 根据id删除部门信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除部门信息")
    @DeleteMapping("/depts/{id}")
    public Results deleteDept(@PathVariable Integer id) {
        boolean b = deptService.deleteDept(id);
        return Results.ok().message("删除成功");
    }

    /**
     * 模糊查询
     *
     * @param name
     * @return
     */
    @ApiOperation("部门名称模糊查询")
    @GetMapping("/depts/deptName")
    public Results getDeptLike(@RequestParam(value = "name") String name) {
        List<Dept> deptLike = deptService.getDeptLike(name);
        return Results.ok().data("deptLike", deptLike);
    }
}
